﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Collections.Specialized;

namespace Press.MVC.Services
{
    public class AspNetAuthorizationService : IAuthorizationService
    {
        #region IAuthorizationService Members

        public AspNetAuthorizationService() { }
        object _lock = null;
        string MTA_ADMIN = "MtaAdmin"; //"MtaAdmin"
        string BLOG_ADMIN = "BlogAdmin";

        public AspNetAuthorizationService(string connectionStringName, string appName)
        {
            if (Roles.Providers.Count == 0)
            {

                //lock it off to avoid collisions
                lock (_lock)
                {

                    SqlRoleProvider provider = new SqlRoleProvider();

                    //The settings
                    NameValueCollection settings = new NameValueCollection();
                    settings.Add("connectionStringName", connectionStringName);

                    //initialize it
                    provider.Initialize(appName, settings);

                    Roles.Providers.Add(provider);

                }
            }
        }

        public bool IsMtaAdmin(string userName)
        {
            return Roles.IsUserInRole(userName, MTA_ADMIN);
        }

        public bool IsBlogAdmin(string userName)
        {
            return Roles.IsUserInRole(userName, BLOG_ADMIN);
           
        }


        #endregion
    }
}
